package com.ple.app.soil;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.print.PageRange;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowContentFrameStats;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.ags.ArcGISImageServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.ple.app.util.GetSnapShot;
import com.ple.app.util.SoilProperties;
import com.ple.app.jzsf.R;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.List;

/**
 * Created by twenj on 2016/11/15.
 * 处理土壤理化数据对应信息，获取数据，并将数据绑定到视图，以及其他相关操作
 */

public class SoilDataActivity extends Activity {

    static int farmID;
    static int cropID;
    static int specificID;

    private Spinner spinnerFarm = null;
    private Spinner spinnerCrop = null;
    private Spinner spinnerSpecificCrop = null;
    private Spinner spinnerSoilProperties = null;

    private Button btnShareSoilMap;
    //地图容器
    private MapView mapView;
    private ArcGISTiledMapServiceLayer tiledMapServiceLayer;
    private ArcGISImageServiceLayer imageServiceLayer;
    private GraphicsLayer graphicsLayer;
    private String wg;
    private String ff;

    //四个数据容器
    ArrayAdapter<String> adapterFarm = null;
    ArrayAdapter<String> adapterCrop = null;
    ArrayAdapter<String> adapterSpecificCrop = null;
    ArrayAdapter<String> adapterSoilProperties = null;

    String[] soilProperties = SoilProperties.SOILPROPERTIES;
    FarmsInfo farmsInfo = new FarmsInfo();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.soil_data_tabs);
        wg = "http://192.168.13.99:6080/arcgis/rest/services/wg/MapServer";
        ff = "http://192.168.13.99:6080/arcgis/rest/services/ff/MapServer";


        //初始化视图
        initView();
        //准备好数据
        initData();
        //初始化事件
        initEvents();

    }

    private void initData() {
        //添加数据

        farmsInfo.setFarmArr(new String[]{"农场A", "农场B"});
        farmsInfo.setCropArr(new String[][]{{"玉米", "大豆", "水稻"}, {"小麦", "高粱", "油菜"}});
        String[][][] specificCrop = new String[][][]{
                {//农场A
                        {"玉米a", "玉米b", "玉米c"},
                        {"大豆1", "大豆2", "大豆3"},
                        {"水稻1", "水稻2", "水稻3"}
                },
                {//农场B
                        
                        {"小麦a", "小麦b", "小麦c"},
                        {"高粱1", "高粱2", "高粱3"},
                        {"油菜1", "油菜2", "油菜3"}
                },
        };
        farmsInfo.setSpecificCropArr(specificCrop);

        //配置适配器
        adapterFarm = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, farmsInfo.getFarmArr());
        adapterCrop = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, farmsInfo.getCropArr()[1]);
        adapterSpecificCrop = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, farmsInfo.getSpecificCropArr()[1][0]);
        adapterSoilProperties = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, soilProperties);

        //spinner banding data
        spinnerFarm.setAdapter(adapterFarm);
        spinnerFarm.setSelection(1, true);
        spinnerCrop.setAdapter(adapterCrop);
        spinnerCrop.setSelection(1, true);
        spinnerSpecificCrop.setAdapter(adapterSpecificCrop);
        spinnerSpecificCrop.setSelection(2, true);
        spinnerSoilProperties.setAdapter(adapterSoilProperties);

        mapView = (MapView) findViewById(R.id.map_soilData);
        //设定Background
        mapView.setMapBackground(Color.parseColor("#ffffff"), Color.TRANSPARENT, 0, 0);
        //为啥setMaxExtent可以起效，setExtent却不行？
        Envelope env = new Envelope(178057.107,3787694.999,253715,3837921);
        mapView.setMaxExtent(env);
    }

    private void initEvents() {
        //spinnerFarm选择器， 与spinnerCrop联动
        spinnerFarm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                adapterCrop = new ArrayAdapter<String>(
                        SoilDataActivity.this,
                        android.R.layout.simple_spinner_item,
                        farmsInfo.getCropArr()[position]);
                spinnerCrop.setAdapter(adapterCrop);
                farmID = position;//记录当前所选农场，用于特定作物
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //spinnerCrop选择器， spinnerSpecificCrop与联动
        spinnerCrop.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                adapterSpecificCrop = new ArrayAdapter<String>(
                        SoilDataActivity.this,
                        android.R.layout.simple_spinner_item,
                        farmsInfo.getSpecificCropArr()[farmID][position]);
                spinnerSpecificCrop.setAdapter(adapterSpecificCrop);
                cropID = position;//记录所选作物品种
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //spinnerSpecificCrop选择器
        spinnerSpecificCrop.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                specificID = position;//记录所选特定作物品种
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //spinnerSoilProperties
        spinnerSoilProperties.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str = (farmsInfo.getFarmArr()[farmID] + farmsInfo.getSpecificCropArr()[farmID][cropID][specificID]).toString();
                switch (position) {
                    case 0: {
                        mapView.removeAll();
                        tiledMapServiceLayer = new  ArcGISTiledMapServiceLayer(ff);
                        mapView.addLayer(tiledMapServiceLayer);
                        break;
                    }
                    case 1: {
                        mapView.removeAll();
                        tiledMapServiceLayer = new ArcGISTiledMapServiceLayer(wg);
                        mapView.addLayer(tiledMapServiceLayer);
                        break;
                    }
                    case 2: {
                        mapView.removeAll();
                        tiledMapServiceLayer = new ArcGISTiledMapServiceLayer(wg);
                        mapView.addLayer(tiledMapServiceLayer);
                        break;
                    }
                    default:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        /**
         * 地图分享功能,保存到sd卡或者分享到微信
         */
        btnShareSoilMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GetSnapShot snapShot = new GetSnapShot(SoilDataActivity.this,mapView);
                snapShot.shareViewToWeChat();//保存截图到微信
                snapShot.saveViewShotToNative();//保存截图到SD卡
                Toast.makeText(SoilDataActivity.this,"保存成功,路径为："+snapShot.getFilepath(),Toast.LENGTH_LONG).show();
            }
        });
    }

    private void initView() {
        //四个Spinner 分别绑定不同数据，使用ArrayAdapter
        spinnerCrop = (Spinner) findViewById(R.id.spinner_crop);
        spinnerFarm = (Spinner) findViewById(R.id.spinner_farm);
        spinnerSpecificCrop = (Spinner) findViewById(R.id.spinner_specificCorp);
        spinnerSoilProperties = (Spinner) findViewById(R.id.spinner_soilProperties);
        //专题图分享
        btnShareSoilMap = (Button) findViewById(R.id.btn_share_soilMap);

        //加载地图
        //mapView = (MapView) findViewById(R.id.map_soilData);
        //tiledMapServiceLayer = new ArcGISTiledMapServiceLayer(tiledServicesUrl);
        //mapView.addLayer(tiledMapServiceLayer);
        //地图容器初始化
    }
}
